﻿package mortal.game.view.copy.groupCopy.item
{
    import Message.Game.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import flash.events.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.groupCopy.data.*;

    public class GroupCopyDefenceBossRewardPanel extends GSprite
    {
        protected var _leftBtn:GLoadedButton;
        protected var _bar:CellsGridSprite;
        protected var _rightBtn:GLoadedButton;
        protected var _itemList:CellsGridSprite;
        private var rwArr:Array;
        private var _currSelIndex:int;
        private var _showNum:int;
        private var _copyId:int = 0;
        private var _container:GSprite;
        private var _maskG:ScaleBitmap;
        private var _rightGap:int = 0;

        public function GroupCopyDefenceBossRewardPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._container = UIFactory.getUICompoment(GSprite, 0, 0, this);
            this._leftBtn = UIFactory.gLoadedButton(ImagesConst.MountLeft_upSkin, 0, 0, 33, 37, this._container);
            this._leftBtn.label = "";
            this._bar = UIFactory.getUICompoment(CellsGridSprite);
            this._bar.colNum = 100;
            this._bar.vCellWid = 120;
            this._bar.hCellWid = 100;
            this._bar.genCellFun = UIFactory.getUICompoment;
            this._bar.genCells(5, GroupCopyBarCell);
            this._bar.x = -24;
            this._bar.y = 71;
            this._container.addChild(this._bar);
            this._rightBtn = UIFactory.gLoadedButton(ImagesConst.MountRight_upSkin, 384, 0, 33, 37, this._container);
            this._rightBtn.label = "";
            this._itemList = UIFactory.getUICompoment(CellsGridSprite);
            this._itemList.colNum = 4;
            this._itemList.vCellWid = 130;
            this._itemList.hCellWid = 100;
            this._itemList.genCellFun = UIFactory.getUICompoment;
            this._itemList.genCells(4, GroupCopyBossReCell);
            this._itemList.x = 29;
            this._itemList.y = 27;
            this._container.addChild(this._itemList);
            this._maskG = UIFactory.bg(0, 0, 418, 140, this, ImagesConst.PanelBg);
            this._container.mask = this._maskG;
            this._leftBtn.configEventListener(MouseEvent.CLICK, this.onCk);
            this._rightBtn.configEventListener(MouseEvent.CLICK, this.onCk);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._rightGap = 0;
            this._currSelIndex = 0;
            this._copyId = 0;
            this._leftBtn = null;
            this._bar = null;
            this._rightBtn = null;
            this._itemList = null;
            return;
        }// end function

        public function initReward(param1:int, param2:int = 5, param3:int = -1, param4:int = -1, param5:int = -1) : void
        {
            this._copyId = param1;
            this.rwArr = GroupCopyUtil.getBoReward(param1);
            this._showNum = param2;
            if (param3 != -1)
            {
                this._itemList.vCellWid = param3;
            }
            if (param4 != -1)
            {
                this._bar.vCellWid = param4;
            }
            if (param5 != -1)
            {
                this._rightGap = param5;
            }
            this._itemList.colNum = this._showNum;
            this._rightBtn.x = 35 + this._showNum * this._itemList.vCellWid - 40 + this._rightGap;
            this._currSelIndex = 0;
            this._itemList.genCells(this._showNum, GroupCopyBossReCell);
            this._bar.genCells((this._showNum + 1), GroupCopyBarCell);
            this._maskG.width = this._rightBtn.x + 33;
            this._container.mask = this._maskG;
            this.selectRwCopy();
            return;
        }// end function

        public function updateListAfterGet() : void
        {
            this.rwArr = GroupCopyUtil.getBoReward(this._copyId);
            var _loc_1:* = this.updateGiftList();
            this.updateBar(this.rwArr, _loc_1);
            return;
        }// end function

        private function updateBar(param1:Array, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:SDefenseReward = null;
            var _loc_6:GroupCopyBarCell = null;
            var _loc_7:int = 0;
            this._bar.genCells((param2 + 1), GroupCopyBarCell);
            var _loc_5:int = 0;
            while (_loc_5 < param1.length && _loc_5 < this._showNum && _loc_5 < param2)
            {
                
                _loc_3 = param1[_loc_5];
                _loc_6 = this._bar.cellArr[_loc_5];
                _loc_7 = int(_loc_3.scoreMax);
                _loc_4 = DefenseInfo.defenseInfo[_loc_7];
                if (_loc_4)
                {
                    if (_loc_4.rewards[_loc_3.scoreMin] == 1 || _loc_4.rewards[_loc_3.scoreMin] == 2)
                    {
                        _loc_6.updateStatu(true);
                    }
                    else
                    {
                        _loc_6.updateStatu(false);
                    }
                }
                else
                {
                    _loc_6.updateStatu(false);
                }
                _loc_5++;
            }
            _loc_6 = this._bar.cellArr[(this._bar.cellArr.length - 1)];
            if (_loc_4 == null || _loc_3 == null)
            {
                return;
            }
            if (_loc_4.rewards[_loc_3.scoreMin] == 1 || _loc_4.rewards[_loc_3.scoreMin] == 2)
            {
                _loc_6.updateStatu(true);
            }
            else
            {
                _loc_6.updateStatu(false);
            }
            return;
        }// end function

        private function initBtnState() : void
        {
            if (this._currSelIndex == 0)
            {
                this._leftBtn.filterEnabled = false;
            }
            else
            {
                this._leftBtn.filterEnabled = true;
            }
            if (this._currSelIndex + this._showNum >= this.rwArr.length)
            {
                this._rightBtn.filterEnabled = false;
            }
            else
            {
                this._rightBtn.filterEnabled = true;
            }
            return;
        }// end function

        private function updateGiftList() : int
        {
            var _loc_1:* = this.getCurData();
            this._itemList.updateAll(_loc_1, GroupCopyBossReCell, "data", false);
            return _loc_1.length;
        }// end function

        private function getCurData() : Array
        {
            var _loc_1:* = this.rwArr.slice(this._currSelIndex, this._currSelIndex + this._showNum);
            return _loc_1;
        }// end function

        private function onCk(event:MouseEvent) : void
        {
            if (this._rightBtn == event.target)
            {
                this._currSelIndex = this._currSelIndex + this._showNum;
                if (this._currSelIndex >= (this.rwArr.length - 1))
                {
                    this._currSelIndex = this.rwArr.length - this._showNum;
                }
            }
            else if (this._leftBtn == event.target)
            {
                this._currSelIndex = this._currSelIndex - this._showNum;
                if (this._currSelIndex < 0)
                {
                    this._currSelIndex = 0;
                }
            }
            this.updateBySelectIndex();
            return;
        }// end function

        private function updateBySelectIndex() : void
        {
            this.updateGiftList();
            this.initBtnState();
            var _loc_1:* = this.getCurData();
            this.updateBar(_loc_1, _loc_1.length);
            return;
        }// end function

        private function selectRwCopy() : void
        {
            var _loc_2:Object = null;
            var _loc_3:SDefenseReward = null;
            var _loc_6:int = 0;
            if (this.rwArr == null)
            {
                return;
            }
            this._currSelIndex = 0;
            var _loc_1:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            while (_loc_5 < this.rwArr.length)
            {
                
                _loc_1++;
                if (_loc_1 > this._showNum)
                {
                    _loc_1 = 1;
                    this._currSelIndex = this._currSelIndex + this._showNum;
                }
                _loc_2 = this.rwArr[_loc_5];
                _loc_6 = int(_loc_2.scoreMax);
                _loc_3 = DefenseInfo.defenseInfo[_loc_6];
                if (_loc_3)
                {
                    if (_loc_3.rewards[_loc_2.scoreMin] == 1)
                    {
                        break;
                    }
                    else
                    {
                        _loc_4++;
                    }
                }
                else
                {
                    _loc_4++;
                }
                _loc_5++;
            }
            if (_loc_4 >= this.rwArr.length)
            {
                this._currSelIndex = 0;
            }
            this.updateBySelectIndex();
            return;
        }// end function

    }
}
